package se.ramfelt.psnfriends.util;

import java.util.Random;
import java.util.StringTokenizer;

public class RandomXorObfuscator {

	private final long seed;

	public RandomXorObfuscator(long seed) {
		this.seed = seed;
	}

	public String perform(String text) {
		if (text == null) {
			return null;
		}
		Random random = new Random(seed);
		StringBuilder builder = new StringBuilder(text.length());
		char[] chars = text.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			builder.append((int)(chars[i] ^ random.nextInt()));
			builder.append(',');
		}
		return builder.toString();
	}
	
	public String reverse(String text) {
		if (text == null) {
			return null;
		}
		StringTokenizer tokenizer = new StringTokenizer(text, ",");
		StringBuilder builder = new StringBuilder();
		Random random = new Random(seed);
		while (tokenizer.hasMoreTokens()) {
			builder.append((char)(Integer.parseInt(tokenizer.nextToken()) ^ random.nextInt()));
		}
		return builder.toString();
	}
}